from etgui import *
from PageLayout import *
from PrefsMirrorEntries import MakeItemIgnoreDirs,MakeItemTabSize

class EnableUpdater:
	ignore=0
	def __call__(self,model,slot):
		model=Cast(model)
		#print "EnableUpdater",self.ignore
		if not self.ignore:
			self.ignore=1
			model.AllValuesChanged()
			self.ignore=0

def MakePageGeneral(model): # important: make changes also in "MakePageGeneralNew" for consistent gui #
	isLibrary=PotGuiToggleButton("IsLibrary","Read Only")
	model.AddModelObserver(EnableUpdater(),"IsLibrary",isLibrary)
 
	projectName=PotTextField("ProjectName")
	projectName.Disable()
 
	projectType=PotGuiPopupButton("ProjectType") # see PotGuiProjModel
	projectType.AddItem("Absolute",0)
	projectType.AddItem("Relative",1)
	projectType.AddItem("Shared",2)
 
	saveAsTemplate=NoMarkButtons(PotGuiOneOf,"SaveAsTemplate")
	saveAsTemplate.AddItem("In Project Description File",0)
	saveAsTemplate.AddItem("As Template",1)
 
	return MakePageLayout(eVObjHExpand|eVObjVCenter,
		MakeBorderedSectionLayout("Project Options",
			Form(
				items=[
					"File Name and Type",HBox(align=eVObjHExpand,items=[projectName,MakeFieldLayout(projectType)]),
					"Project Directory",PotExpandedTextField(model,"ProjectDir") # see PotGuiProjModel
				]
			),
			isLibrary, 
			PotGuiToggleButton("UpdateSWSimmediatly","Update Shared Project Directory Immediately"),
			PotGuiToggleButton("BrowsingOnly","Do not Create Symbolic Links to Source Files")
		),
		MakeBorderedSectionLayout("Save Current Project Attribute Settings",saveAsTemplate)
	)

def MakePageGeneralNew(model): # prefix "New" for slot 'args' #
	projectName=PotTextField("New.PDFName")
	projectType=PotGuiPopupButton("ProjectType") # see PotGuiProjModel
	projectType.AddItem("Absolute",0)
	projectType.AddItem("Relative",1)
	projectType.AddItem("Shared",2)
 
	followSymbolicLinks=Filler(gPoint1)
	if IsWindows()==0:
		followSymbolicLinks=PotGuiToggleButton("New.FollowSymlinks","Follow Symbolic Links")
 
	saveAsTemplate=PotGuiOneOf("SaveAsTemplate")
	saveAsTemplate.AddItem("In Project Description File",0)
	saveAsTemplate.AddItem("As Template",1)
 
	return MakePageLayout(eVObjHExpand|eVObjVCenter,
		MakeBorderedSectionLayout("Project Options",
			Form(
				items=[
					"File Name and Type",HBox(align=eVObjHExpand,items=[projectName,MakeFieldLayout(projectType)]),
					"Project Directory",PotExpandedTextField(model,"ProjectDir") # see PotGuiProjModel
				]
			),
			PotGuiToggleButton("IsLibrary","Read Only"), 
			PotGuiToggleButton("UpdateSWSimmediatly","Update Shared Project Directory Immediately"),
			PotGuiToggleButton("BrowsingOnly","Do not Create Symbolic Links to Source Files")
		),
		MakeBorderedSectionLayout("New Project Options",
			Form(
				items=MakeItemIgnoreDirs(model,"New.IgnoreDirs")+
					["Destination Directory",PotTextField("New.PDFDestination")]
			),
			PotGuiToggleButton("New.RemoveEmpty","Ignore Directories without Project Specific Files"),
			PotGuiToggleButton("New.Recursive","Generate Subproject Tree"),
			followSymbolicLinks,
			PotGuiToggleButton("New.UnifyProjects","Generate Unique Project Names")
		),
		MakeBorderedSectionLayout("Save Current Project Attribute Settings",saveAsTemplate)
	)

def MakePageGeneralAdvanced(model):
	enablePath="UseLocationStateFile"
	enableItem=Form(
		items=[
			" Other Directory",PotExpandedTextField(model,"LocationStateFile")
		]
	)
	FieldEnabler(model,enablePath,
		items=[
			enableItem
		]
	)
	dir=PotGuiOneOf("UseLocationStateFile")
	dir.AddItem("Use Generated Files Directory",0)
	dir.AddVObject(enableItem,1)
 
	return MakePageLayout(eVObjHExpand|eVObjVCenter,
		MakeBorderedSectionLayout("SNiFF+ Generated Files",
			Form(
				items=[
					"Generated Files Directory",PotExpandedTextField(model,"GenerateDir")
				]
			)
		),
		MakeBorderedSectionLayout("Tool Status Files",
			dir
		),
		MakeBorderedSectionLayout("Other Options",
			Form(
				items=MakeItemTabSize(model,"TabLength")
			)
		)
	)
